//
//  STAlertHelper.swift
//  iPhone
//
//  Created by Toj on 5/27/22.
//

import UIKit

class STAlertHelper: NSObject {
    
    static let manager = STAlertHelper()
    
    private weak var alert: STInnerAlertController?
    public class func showAlert(alerModel: STAlertModel, in vc: UIViewController? = nil, defaultAction: (() -> Void)? = nil, cancelAction: (() -> Void)? = nil, effectAction: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
        
        let nextPresent = {
            let alert = STInnerAlertController()
            alert.alertModel = alerModel
            alert.defalutAction = defaultAction
            alert.cancelAction = cancelAction
            alert.effectAction = effectAction
            
            var fromVC = vc
            if fromVC == nil {
                let appDelegate = UIApplication.shared.delegate as? AppDelegate
                fromVC = appDelegate?.window?.rootViewController
            }
            fromVC?.present(alert, animated: false, completion: completion)
            manager.alert = alert
        }
        
        guard let oldAlert = manager.alert else {
            nextPresent()
            return
        }
        
        oldAlert.dismiss(animated: false, completion: {
            nextPresent()
        })
    }
}

extension STAlertHelper {
    private var keyWindow: UIView? {
        return UIApplication.shared.keyWindow
    }
    
    func show(alert view: UIView, inView: UIView? = nil) {
        var superView = inView
        if superView == nil {
            superView = UIApplication.shared.keyWindow
        }
        guard let rootView = superView else { return }
        rootView.addSubview(view)
        view.snp.makeConstraints { make in
            make.edges.equalTo(rootView)
        }
    }
    
    /** 弹框
     * title: 标题
     * message: 内容
     * defalutButton: 默认按钮文案, 可以为nil, 有默认文案ok
     * cancelButton: 取消按钮文案, 可以为nil, 有默认文案cancel
     * isCancel: 取消按钮是否显示
     * isRootView: 是否是全局, 默认true
     * inView: 在哪个View展示, 只有 isRootView = false才有效
     * defaultAction: 点击默认按钮, 默认是ok
     * cancelAction: 点击cancel按钮, 默认是cancel
     * effectAction: 点击空白区域, 背景地区
     */
    func show(message: String,
              title: String = "",
              defalutButton: String = "",
              cancelButton: String = "",
              isCancel: Bool = false,
              inView: UIView? = nil,
              defaultAction: (() -> Void)? = nil,
              cancelAction: (() -> Void)? = nil,
              effectAction: (() -> Void)? = nil) {
        var model = STAlertModel()
        model.isCancel = isCancel
        model.title = title
        model.popDescription = message
        model.popBtn = defalutButton
        model.popCancelBtn = cancelButton
        show(alerModel: model, inView: inView, defaultAction: defaultAction, cancelAction: cancelAction, effectAction: effectAction)
    }
    
    // 全局
    public func show(alerModel: STAlertModel,
                     inView: UIView? = nil,
                     defaultAction: (() -> Void)? = nil,
                     cancelAction: (() -> Void)? = nil,
                     effectAction: (() -> Void)? = nil) {
        let count = UIViewController.vc_currentNavigation?.viewControllers.count ?? 0
        var fromView = inView ?? keyWindow
        if count > 1 && inView == nil {
            fromView = UIViewController.vc_currentNavigationTopViewController?.view
        }
        
        var backgroundColor = UIColor.black.withAlphaComponent(0.6)
        let views = fromView?.subviews.reversed() ?? []
        for view in views {
            if !(view is STAlert) { continue}
            backgroundColor = UIColor.clear
            break
        }
        // print("-->toj: show:\(alerModel.title)")
        let alert = STAlert(model: alerModel)
        alert.alertBackgroundColor = backgroundColor
        alert.defalutAction = defaultAction
        alert.cancelAction = cancelAction
        alert.effectAction = effectAction
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            guard let from = fromView else { return }
            STAlertHelper.manager.show(alert: alert, inView: from)
        }
    }
}
